package net.yuxianghe;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/**
 * Listener
 * Created by hxy on 16/12/26.
 */
public class Listener {
    public static void main(String[] args) {
        int port = 19099;
        String address = "224.0.1.199";
        byte data[] = new byte[1024];
        try {
            System.out.println("start...");
            InetAddress inetAddress = InetAddress.getByName(address);
            MulticastSocket multicastSocket = new MulticastSocket(port);
            multicastSocket.joinGroup(inetAddress);
            DatagramPacket datagramPacket = new DatagramPacket(data, data.length);
            System.out.println("receive...");
            multicastSocket.receive(datagramPacket);
            String message = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
            System.out.println("receive message: " + message);
            System.out.println("success");
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

